package com.android.videotest;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

/**
 * 项目名称：supermarket
 * 类 名 称：AnimationUtils
 * 类 描 述：动画工具类
 * 创建时间：2019-11-14 14:44
 * 创 建 人：sansecy
 */
public class AnimationUtils {

    /**
     * 将初始对象移动并缩放到目标位置
     *  @param origin
     * @param target
     * @param animateDuration
     */
    @SuppressLint("ObjectAnimatorBinding")
    public static void scaleTranslateTarget(View origin, View target, int animateDuration) {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("X", origin.getX(), target.getX());
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("Y", origin.getY(), target.getY());
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofInt("width", origin.getWidth(), target.getWidth());
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofInt("height", origin.getHeight(), target.getHeight());
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(origin, holder1, holder2, holder3, holder4);
        objectAnimator.setDuration(animateDuration);
        objectAnimator.setAutoCancel(true);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue("width");
                int height = (int) animation.getAnimatedValue("height");
//                if (origin.getWidth() == width && origin.getHeight() == height) {
//                    return;
//                }
                ViewGroup.LayoutParams layoutParams = origin.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                origin.setLayoutParams(layoutParams);
            }
        });
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }

    public static void scaleView(View target, int originScale, int targetScale, int duration, Animator.AnimatorListener listener) {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("ScaleX", originScale, targetScale);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("ScaleY", originScale, targetScale);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(target, holder1, holder2);
        objectAnimator.setDuration(duration);
        objectAnimator.setAutoCancel(true);
        if (listener != null) {
            objectAnimator.addListener(listener);
        }
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }
}
